package com.pig4cloud.pig.module.service.devices.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.module.api.entity.DeviceDrawRecord;
import com.pig4cloud.pig.module.api.entity.devices.DevicesEggGetRecord;
import com.pig4cloud.pig.module.api.entity.excel.DevicesGetRecordExcel;
import com.pig4cloud.pig.module.mapper.devices.DeviceDrawRecordMapper;
import com.pig4cloud.pig.module.mapper.devices.DevicesEggGetRecordMapper;
import com.pig4cloud.pig.module.service.devices.IDevicesEggGetRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 鸡蛋柜领取记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-06
 */
@Service
public class DevicesEggGetRecordServiceImpl extends ServiceImpl<DevicesEggGetRecordMapper, DevicesEggGetRecord> implements IDevicesEggGetRecordService {

	@Resource
	private DevicesEggGetRecordMapper eggGetRecordMapper;
	@Resource
	private DeviceDrawRecordMapper deviceDrawRecordMapper;

	@Override
	public Page<DevicesEggGetRecord> selectCellRecordList(Page<DevicesEggGetRecord> page, Map<String, Object> map) {
		return eggGetRecordMapper.selectCellRecordList(page, map);
	}

	@Override
	public List<DevicesEggGetRecord> selectCellRecordList(Map<String, Object> map) {
		return eggGetRecordMapper.selectCellRecordList(map);
	}

	@Override
	public Page<DevicesGetRecordExcel> selectTongjiByDeviceId(Page<DevicesGetRecordExcel> page, Map<String, Object> map) {
		return eggGetRecordMapper.selectTongjiByDeviceId(page,map);
	}

	@Override
	public List<DevicesGetRecordExcel> selectTongjiByDeviceId(Map<String, Object> map) {
		return eggGetRecordMapper.selectTongjiByDeviceId(map);
	}


	/**
	 * 按照时间段类型来统计 各类型下的数据
	 * @param type  0 - 按照月为单位统计   1 - 按照 周 为单位 统计
	 * @return
	 */
	@Override
	public Map<String, Object> getGerRecordCharts(int type) {
		Map<String,Object> map = new HashMap<>();
		List<String> times = new ArrayList<>();
		List<Long> chouzhi = new ArrayList<>();
		List<Long> mask = new ArrayList<>();
		List<Long> packageList = new ArrayList<>();
		//
		if(type == 0){
			for (int i = 0;i < 12;i++){
				int month = i + 1;
				times.add(month + "月");
				String monthStr = (month < 10 ? "0" + month : month) + "";
				String beginTime = DateUtils.getYear() + "-" + monthStr +"-01 00:00:00";
				String endTime = DateUtils.getLastDayOfMonth(month) +" 23:59:59";

				System.out.println("beginTime:  == " + beginTime);
				System.out.println("endTime:  == " + endTime);

				QueryWrapper<DevicesEggGetRecord> qw = new QueryWrapper<>();
				qw.between("create_time",beginTime,endTime);
				//格子的箱格类型  0  - 鸡蛋 1 - 抽纸   2- 香皂 3 - 洗衣粉
				qw.eq("cell_type",1);
				long count = eggGetRecordMapper.selectCount(qw);
				chouzhi.add(count * 3);



				//口罩
				QueryWrapper<DeviceDrawRecord> qw1 = new QueryWrapper<>();
				qw1.between("create_time",beginTime,endTime);
				//取的类型 0 - 袋子 1 - 口罩
				qw1.eq("get_type",1);
				long countMask = deviceDrawRecordMapper.selectCount(qw1);
				mask.add(countMask * 3);


				//口罩
				QueryWrapper<DeviceDrawRecord> qw2 = new QueryWrapper<>();
				qw2.between("create_time",beginTime,endTime);
				//取的类型 0 - 袋子 1 - 口罩
				qw2.eq("get_type",0);
				long countPackage = deviceDrawRecordMapper.selectCount(qw2);
				packageList.add(countPackage * 3);
			}
		}
		map.put("times",times);
		map.put("mask",mask);// 口罩数   数组形式
		map.put("package",packageList);// 环保袋数  数组形式
		map.put("chouzhi",chouzhi);// 抽纸数  数组形式
		return map;
	}
}
